/**
  ******************************************************************************
  * @file    gt32f030_uart.c
  * @author  GT Application Team
  * @version V1.0.0
  * @date    03-January-2025
  *       
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2022 Giantec Semicondutor Inc</center></h2>
  *
  *             http://www.giantec-semi.com/
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __GT32F030_UART_H
#define __GT32F030_UART_H

#ifdef __cplusplus
 extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "gt32f030.h"
/** 
  * @brief UART Init Structure definition  
  */ 
typedef struct
{
  unsigned int UART_BaudRate;              /*!< This member configures the UART communication baud rate. */
	
	unsigned int UART_BaudRateDouble;         /*!< Baud Rate double enable*/
	
  unsigned int UART_Mode;                   /*!< Specifies wether the Receive or Transmit mode is enabled or disabled.
                                          This parameter can be a value of UART_Mode */
	
} UART_InitTypeDef;

/** 
  * @brief UART_IRD Init Structure definition  
  */ 
typedef struct
{
  unsigned int UART_IRDIRLPMOD;            /*!< This member configures the UART communication baud rate.
                                           The baud rate is computed using the following formula:
                                            - IntegerDivider = ((PCLKx) / (16 * (UART_InitStruct->UART_BaudRate)))
                                            - FractionalDivider = ((IntegerDivider - ((u32) IntegerDivider)) * 16) + 0.5 */
	
	unsigned int UART_IRDIRRXINV;            /*!<IRD  receiving Data Flip Control Bit*/

	unsigned int UART_IRDIRTXINV;            /*!<IRD  send Data Flip Control Bit*/
	
  unsigned int UART_IRDPSC;                /*!< UART IRD PSC
                                           This parameter can be a value of UART_Mode */
} UART_IRDInitTypeDef;


/** @defgroup UART_Exported_Constants
  * @{
  */   
#define IS_UART_ALL_PERIPH(PERIPH) 		(((PERIPH) == UART0) ||\
                                    	 ((PERIPH) == UART1))

/** @defgroup UART_Clock 
  * @{
  */ 
#define UART_Clock_Disable              ((unsigned int)0x00000000)
#define UART_Clock_Enable               ((unsigned int)0x00000800)
#define IS_UART_CLOCK(CLOCK) 			(((CLOCK) == UART_Clock_Disable) ||\
                               			 ((CLOCK) == UART_Clock_Enable))
															 																													 
/** @defgroup UART_MODE
  * @{
  */ 															 
#define UART_Mode_0       		((uint32_t)0x00000000)   
#define UART_Mode_1       		((uint32_t)0x00000001)    
#define UART_Mode_2       		((uint32_t)0x00000002)    
#define UART_Mode_3       		((uint32_t)0x00000003)     
#define IS_UART_MODE(MODE) 		(((MODE) == UART_Mode_0) ||\
                            	 ((MODE) == UART_Mode_1) ||\
                               ((MODE) == UART_Mode_2) ||\
															 ((MODE) == UART_Mode_3))

#define UART_FrameMode_Tx       		((uint32_t)0x00000000)   
#define UART_FrameMode_Rx       		((uint32_t)0x00000010) 
#define UART_FrameMode_TxRx       	((uint32_t)0x00000010)    
    
#define IS_UART_FRAMEMODE(MODE) (((MODE) == UART_FrameMode_Tx)  ||\
																 ((MODE) == UART_FrameMode_Rx)  ||\
																 ((MODE) == UART_FrameMode_TxRx))


#define UART_IRDACR_IRRXINV       ((uint32_t)0x00000400)   /*!< IrRXD 数据反转位使能 */
#define UART_IRDACR_IRTXINV       ((uint32_t)0x00000200)   /*!< IrTXD 数据反转位使能 */
#define UART_IRDACR_IREN          ((uint32_t)0x00000100)   /*!< IrDA使能  */

/****************** Bit definition for USART_SCON register  *******************/
#define UART_CR_RIEN            0x00000001   /*!< 接收完成中断使能 */
#define UART_CR_TIEN            0x00000002   /*!< 发送完成中断使能 */
#define UART_CR_RB8             0x00000004   /*!< 接收 RB8 位 */
#define UART_CR_TB8             0x00000008   /*!< 发送 TB8 位 */
#define UART_CR_REN             0x00000010   /*!< 接收/发送 */
#define UART_CR_SM2             0x00000020   /*!< 多主机通信 */
#define UART_CR_SM1             0x00000040   /*!< 工作模式 */
#define UART_CR_SM0             0x00000080   /*!< 工作模式 */
#define UART_CR_FEEN            0x00000100   /*!<  */
#define UART_CR_DBAUD           0x00000200   /*!< 双倍波特率 */
#define UART_Mode_Mask          		((uint32_t)0x0000C0)  /*!< USART address Mask */


/****************** Bit definition for USART_ISR register  *******************/
#define UART_INTSR_RI               0x00000001   /*!< 接收完成中断标志位   */ 
#define UART_INTSR_TI               0x00000002   /*!< 发送完成中断标志位   */
#define UART_INTSR_FEI              0x00000004   /*!< 接收帧错误中断标志位   */
#define UART_INTSR_TxBuffer_Empty   0x00000008   /*!< 发送缓存空中断标志位 */

/****************** Bit definition for USART_ICR register  *******************/
#define UART_INTCLR_RI               0x00000001
#define UART_INTCLR_TI               0x00000002
#define UART_INTCLR_FEI              0x00000004


#define UART_SELF_BRG            ((unsigned int)0x00010000)
#define UART_RIEN_ENABLE          ((unsigned int)0x00000001)    /*!< UART接收中断使能 */
#define UART_TIEN_ENABLE          ((unsigned int)0x00000002)    /*!< UART发送中断使能 */
#define IS_UART_IT(IT)           ((IT == UART_RIEN_ENABLE)||\
                                  (IT == UART_TIEN_ENABLE))
																						
#define  UART_TIM_EABLE          ((unsigned int)0x000000E0)


/** @defgroup UART_Exported_Functions
  * @{
  */
void UART_DeInit(UART_TypeDef* UARTx);
void UART_Init(UART_TypeDef* UARTx, UART_InitTypeDef* UART_InitStruct);
void UART_StructInit(UART_InitTypeDef* UART_InitStruct);
//void UART_Cmd(UART_TypeDef* UARTx, uint16_t FuncState, FunctionalState NewState);
void UART_ITConfig(UART_TypeDef* UARTx, unsigned int UART_IT, FunctionalState NewState);
void UART_FrameModeConfig(UART_TypeDef* UARTx, unsigned int FrameMode);
//void UART_SetAddress(UART_TypeDef* UARTx, uint8_t UART_Address, uint8_t UART_Address_Enable);
//void UART_ReciveCmd(UART_TypeDef* UARTx, UART_InitTypeDef* UART_InitStruct, FunctionalState NewState);
void UART_SendData(UART_TypeDef* UARTx, unsigned char Data);
unsigned char UART_ReceiveData(UART_TypeDef* UARTx);
//void UART_IrDAConfig(UART_TypeDef* UARTx, UART_IRDInitTypeDef* UART_IRDInitStruct);
//void UART_IrDACmd(UART_TypeDef* UARTx, FunctionalState NewState);
ITStatus UART_GetITStatus(UART_TypeDef* UARTx, unsigned int UART_IT);
void UART_ClearIT(UART_TypeDef* UARTx, unsigned int UART_IT);
//void UART_TB8Cmd(UART_TypeDef* UARTx, FunctionalState NewState);
//void UART_RB8Cmd(UART_TypeDef* UARTx, FunctionalState NewState);


#ifdef __cplusplus
}
#endif

#endif /* __GT32F030_UART_H */

